package com.ajax.servlet;

import com.ajax.bean.User;
import com.ajax.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * 控制层（C） -- controller
 */
@WebServlet(urlPatterns = "/getUser")
public class UserServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.    获取参数
        String uid = req.getParameter("uid");
        String userName = req.getParameter("username");
        String password = req.getParameter("password");
        String phone = req.getParameter("phone");
        //2.    调取service
        UserServiceImpl service = new UserServiceImpl();
        List<User> list = service.getAll();
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        String text = null;
        for (User user : list) {
            //System.out.println(user.getId()+","+user.getUname()+","+user.getUpass()+","+user.getUphone());
            String uname = user.getUname();
            if (uname.equals(userName)){
                text = "用户已存在，可直接登录";
                break;
            }else {
                text = "用户不存在，请先进行注册";
            }
        }
        writer.print(text);
        //3.    跳转页面
        //后台传递数据给前台
//        req.setAttribute("userlist",list);
        //req.getRequestDispatcher("/user.jsp").forward(req,resp);

    }
}
